<?php

session_start();
$num = 4;
$height = 100;
$width = $height * $num;
$zfj = 'qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM'; //L=62
$yanz = '';
$img = imagecreatetruecolor($width, $height);
$color1 = imagecolorallocate($img, 204, 204, 204);

imagefill($img, 0, 0, $color1);
for ($i = 0; $i < $num; $i++) {     //验证码
    $tmp = $zfj[mt_rand(0, 61)];
    $yanz = $yanz . $tmp;
    $color = imagecolorallocate($img, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
    imagettftext($img, $height * 0.7, mt_rand(-20, 20), $height * $i + $height * 0.25, $height * 0.66, $color, 'yahei.ttf', $tmp);
}
/*for ($i = 0; $i < 100; $i++) {    //干扰字符
    $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imagettftext($img, $height * 0.1, mt_rand(-20, 20), mt_rand(0, $width), mt_rand(0, $height), $color, 'yahei.ttf', $zfj[mt_rand(0, 61)]);
}*/
for ($i = 0; $i < 100; $i++) {   //干扰杂点
    $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), $color);
}
for ($i = 0; $i < 10; $i++) {   //干扰杂线段
    $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imageline($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $color);
}
header('Content-type:image/png');
imagepng($img);
imagedestroy($img);
$_SESSION['yanz'] = $yanz;
?>